from contextlib import asynccontextmanager

import uvicorn
from fastapi import FastAPI

from server.routers.api import api_router
from server.scheduler import init_scheduler, scheduler, cleanup_redis
from server.etc.settings import settings



# @asynccontextmanager
# async def lifespan(app: FastAPI):
#     # FastAPI 启动完成后执行
#     print("FastAPI 已完全启动")
#     init_scheduler()  # 此时启动定时任务
#     yield
#     # 关闭逻辑
#     scheduler.shutdown(wait=False)
#     cleanup_redis()  # 新增的清理操作
#
# app = FastAPI(lifespan=lifespan)
#
# @app.get("/jobs")
# def list_jobs():
#     jobs = scheduler.get_jobs()
#     return {"jobs": [str(job) for job in jobs]}
app = FastAPI(
    title=settings.PROJECT_NAME,
    version=settings.PROJECT_VERSION,
    debug=settings.DEBUG,
)

app.include_router(api_router, prefix="/api")

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8880, reload=True)